package edu.whu.notepal.controller;

import edu.whu.notepal.dto.Result;
import edu.whu.notepal.pojo.Contacts;
import edu.whu.notepal.service.ContactsService;
import edu.whu.notepal.service.UserService;
import edu.whu.notepal.utils.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
 * @description 通讯录好友的控制器类
 * @author 陈磊
 */
@RestController
@RequestMapping("/contacts")
public class ContactsController {

    @Resource
    UserService userService;

    @Resource
    MyWebSocket myWebSocket;

    @Resource
    ContactsService contactsService;

    @Resource
    RedisClient redisClient;

    /**
     * 同意加好友
     * @param friendId
     * @return
     */
    @PostMapping("/accept")
    public Result<String> accept(Long friendId,Boolean accept){
        if(accept){
            contactsService.saveBiDirection(UserHolder.getUser().getId(),friendId);
            return Result.success("加好友成功");
        }else {
            myWebSocket.sendToOne(SocketBeanFactory.instance
                    (UserHolder.getUser().getId(),friendId,MessageType.FRIEND_REJECT,"您的好友申请被拒绝了",null));
        }
        return Result.success("您已拒绝好友申请");
    }

    @PostMapping("/add")
    public Result<String> add(Long friendId){
        Long fromUser = UserHolder.getUser().getId();
        myWebSocket.sendToOne(SocketBeanFactory.
                instance(fromUser,friendId,MessageType.FRIEND_REQUEST,"来自"+userService.getById(fromUser).getName()+"的好友申请",null));
        return Result.success("好友申请已发出！");
    }

    @GetMapping("/my")
    public Result<List<Contacts>> myContacts(){
        String key = RedisConstants.CONTACTS_USER_KEY + UserHolder.getUser().getId();
//        if(redisClient.hasKey(key)){
//            List<Contacts> list = redisClient.getList(key, 0, -1, Contacts.class);
//            return Result.success(list);
//        }
        List<Contacts> friends = contactsService.getFriendsById(UserHolder.getUser().getId());
        redisClient.saveList(key,friends,RedisConstants.CONTACTS_USER_TTL, TimeUnit.MINUTES);
        return Result.success(friends);
    }

    @DeleteMapping
    public Result<String>deleteFriend(Long friendId){
        redisClient.deleteKey(RedisConstants.CONTACTS_USER_KEY+friendId);
        redisClient.deleteKey(RedisConstants.CONTACTS_USER_KEY+UserHolder.getUser().getId());
        contactsService.deleteBiDirection(UserHolder.getUser().getId(),friendId);
        myWebSocket.sendToOne(SocketBeanFactory.
                instance(UserHolder.getUser().getId(),friendId,MessageType.FRIEND_DELETE,"您已被删除",null));
        return Result.success("删除好友成功!");
    }

    @GetMapping("/isFriend")
    public Result<Boolean>isFriend(Long friendId){
        return Result.success(contactsService.judgeFriend(UserHolder.getUser().getId(),friendId));
    }

    @GetMapping("/online")
    public Result<Boolean>online(Long friendId){
        return Result.success(myWebSocket.isOnline(friendId));
    }

}
